import { reqGetOrderPayResult } from '@/api/order/order.js'
import { openPage } from '@/utils/utils'
import small from '@/smallapp/small'

export default {
  wxOrderPay(sendData, orderId, redirect_url, dontJump) {
    const payData = JSON.parse(sendData)
    let that = this
    // console.log('场景值', small.wxCache('scene'))
    // console.log('支付参数', payData)
    if (payData.is_mini_program_pay == 1 || (small.globalData.isJyZj == 1 && (small.wxCache('scene') == 1175 || small.wxCache('scene') == 1177 || small.wxCache('scene') == 1195))) {
      // console.log('新版支付')
      wx.requestOrderPayment({
        timeStamp: payData.timeStamp,
        nonceStr: payData.nonceStr, //
        package: payData.package, //
        signType: payData.signType, //微信签名方式
        paySign: payData.paySign, //微信签名
        orderInfo: payData.orderInfo,
        success: (res) => {
          // console.log(res)
          // TODO 如果带了redirect_url 直接跳转
          if (redirect_url) {
            openPage(redirect_url, 1)
          } else {
            that.wxPaySuccessFun(orderId, 1)
          }
        },
        fail: (failmsg) => {
          // console.log('支付错误', failmsg)
          // 如果带了redirect_url 直接跳转
          if (redirect_url) {
            openPage(redirect_url, 1)
          } else {
            that.wxPaySuccessFun(orderId, 0, dontJump)
          }
        }
      })
    } else {
      wx.requestPayment({
        timeStamp: payData.timeStamp,
        nonceStr: payData.nonceStr, //
        package: payData.package, //
        signType: payData.signType, //微信签名方式
        paySign: payData.paySign, //微信签名
        success: (res) => {
          // console.log(res)
          // TODO 如果带了redirect_url 直接跳转
          if (redirect_url) {
            openPage(redirect_url, 1)
          } else {
            that.wxPaySuccessFun(orderId, 1)
          }
        },
        fail: (failmsg) => {
          // console.log('支付错误',failmsg)
          // 如果带了redirect_url 直接跳转
          if (redirect_url) {
            openPage(redirect_url, 1)
          } else {
            that.wxPaySuccessFun(orderId, 0, dontJump)
          }
        }
      })
    }
  },

  /**
   * 支付成功回调方法
   * @param {*} orderId 订单ID
   * @param {*} type  0 支付失败 1 支付成功
   * @param {*} dontJump 传了值代表不跳转
   */
  wxPaySuccessFun(orderId, type, dontJump) {
    if (dontJump) return false
    reqGetOrderPayResult({order_id: orderId, pay_result: type}).then(res => {
      if (res.status == 1) {
        // 如果是拼团商品则跳转拼团成功页面
        if (res.data.shareGroup && res.data.shareGroup == 1 && res.data.shareGroupUrl && type == 1) {
          openPage(res.data.shareGroupUrl, 1)
          return false
        }
        // 如果后台返回了链接则跳转到后台返回的链接
        if (res.data && res.data.redirect_url) {
          openPage(res.data.redirect_url, 1)
        } else {
          if (type == 1) {
            res.data.id ? orderId = res.data.id : ''
            window.open('/buySuccess?id=' + orderId)
          } else {
            window.location.href = '/order-lists'
          }
        }
      }
    })
  }
}
